home *** CD-ROM | disk | FTP | other *** search
- /* DrawEPSF.c */
- /* Copyright 1992, Gary D. McGath */
-
- extern void InitOutBuf(void);
- extern void OutputString(char *str, int theFile);
- extern void OutputChar(int ch,int theFile);
- extern void OutputNum(int n, int theFile);
- extern void FlushOutBuf(int theFile);
-
- OSErr CreateEPSFFile(SFReply *theReply, int *theFile);
- void DrawEPSF(PicHandle han, int theFile);
- void WriteBoundingBox(PicHandle han,Rect *bbRect, int theFile);
- void WritePrologue(int theFile);
- void FlipCoords(Rect *RectP, int theFile);
- void WriteEpilogue(int theFile);
-
-
- /* The strings of the prologue. To conserve data space, these could
- be put into a resource. */
- char *prologueStrs[] = {
- "/_E_save save def\r",
- "/our_dict 70 dict def\r",
- "our_dict begin /bd {bind def} def /xd {exch def} bd\r",
- "/rdims {/bt xd /rt xd /tp xd /lf xd} bd\r",
- "/fradj {/tp tp phw add def /bt bt phw sub def\r",
- " /lf lf phw add def /rt rt phw sub def} bd\r",
- "/arctopppp {arcto pop pop pop pop} bd\r",
-
- /* fill rect proc: l t r b flrec */
- "/flrec {rdims lf tp moveto rt tp lineto rt bt lineto lf bt lineto closepath fill} bd\r",
-
- /* frame rect proc: l t r b pen.h pen.v frrec */
- "/frrec {/pnh xd /pnw xd rdims\r",
- " lf tp moveto rt tp lineto rt bt lineto lf bt lineto closepath\r",
- " /lf lf pnw add def /tp tp pnh add def /rt rt pnw sub def /bt bt pnh sub def\r",
- " lf tp moveto rt tp lineto rt bt lineto lf bt lineto closepath\r",
- " eofill} bd\r",
-
- /* fill oval proc: l t r b floval */
- "/floval {rdims gsave lf rt add 2 div tp bt add 2 div translate\r",
- " rt lf sub 2 div bt tp sub 2 div scale\r",
- " 1 0 moveto 0 0 1 0 360 arc fill grestore} bd\r",
-
- /* frame oval proc: l t r b pen.h froval */
- "/froval {dup /pnw xd setlinewidth",
- "/phw pnw 0.5 mul def fradj",
- " rdims /mt matrix currentmatrix def lf rt add 2 div tp bt add 2 div translate\r",
- " rt lf sub 2 div bt tp sub 2 div scale\r",
- " 1 0 moveto 0 0 1 0 360 arc mt setmatrix stroke} bd\r",
-
- /* frame arc proc: l t r b pen.h startangle arcangle frarc */
- "/frarc {/arca xd /stra xd setlinewidth rdims\r",
- " /mt matrix currentmatrix def lf rt add 2 div tp bt add 2 div translate\r",
- " rt lf sub 2 div bt tp sub 2 div scale\r",
- " 0 0 1 stra 90 sub dup arca add arc mt setmatrix stroke} bd\r",
-
- "/flarc {/arca xd /stra xd rdims\r",
- " gsave lf rt add 2 div tp bt add 2 div translate\r",
- " rt lf sub 2 div bt tp sub 2 div scale\r",
- " 0 0 moveto 0 0 1 stra 90 sub dup arca add arc fill grestore} bd\r",
-
- /* frame round rect proc: l t r b pen.h rad.h
- This version makes the simplifying assumptions of a square pen and
- circular (not elliptical) corners. */
- "/frrrect {/rd xd /pnw xd rdims /phw pnw 0.5 mul def fradj\r",
- " rt rd sub tp moveto rt tp rt tp rd add rd arctopppp\r",
- " rt bt rt rd sub bt rd arctopppp\r",
- " lf bt lf bt rd sub rd arctopppp\r",
- " lf tp lf rd add tp rd arctopppp\r closepath stroke} bd\r",
-
- /* fill round rect: l t r b rad.h */
- "/flrrect {/rd xd rdims\r",
- " rt rd sub tp moveto rt tp rt tp rd add rd arctopppp\r",
- " rt bt rt rd sub bt rd arctopppp\r",
- " lf bt lf bt rd sub rd arctopppp\r",
- " lf tp lf rd add tp rd arctopppp\r closepath fill} bd\r",
- "" /* last string must be null */
- };
-
- char *epilogueStrs[] = {
- "end _E_save restore\r",
- ""
- };
-
- int thePSResFile;
-
-
- /* Call this before calling DrawEPSF. */
- OSErr CreateEPSFFile(SFReply *theReply, int *theFile)
- {
- OSErr err;
- FSDelete(&theReply->fName[0],theReply->vRefNum); /* delete any old file */
- err = Create(&theReply->fName[0], theReply->vRefNum, '????', 'EPSF');
- err = FSOpen(&theReply->fName[0], theReply->vRefNum, theFile);
- SetVol((StringPtr) 0, theReply->vRefNum);
- CreateResFile(&theReply->fName[0]);
- thePSResFile = OpenResFile(&theReply->fName[0]);
- if (err != noErr)
- *theFile = 0;
- return err;
- }
-
- /* The main routine for writing the data fork of the EPSF file. */
- void DrawEPSF(PicHandle han, int theFile)
- {
- GrafPtr savePort;
- GrafPtr dstPort;
- Rect dstRect;
- Rect *picRectPtr;
- InitOutBuf();
- OutputString("%!PS-Adobe-3.0 EPSF-3.0\r", theFile); /* doesn't really belong here */
- picRectPtr = &(**han).picFrame;
- dstRect.left = 0;
- dstRect.top = 0;
- dstRect.right = picRectPtr->right;
- dstRect.bottom = picRectPtr->bottom;
- WriteBoundingBox(han, &dstRect,theFile);
- WritePrologue(theFile);
- FlipCoords(&dstRect, theFile);
- GetPort(&savePort);
- dstPort = (GrafPtr) NewPtr(sizeof(GrafPort));
- if (dstPort == 0)
- return;
- OpenPort(dstPort);
- psdInitPort(dstPort);
- psdSetupProcs(dstPort); /* set up port and bottleneck procs */
- SetPort((GrafPtr)dstPort);
- DrawPicture(han,picRectPtr);
-
- WriteEpilogue(theFile);
- FlushOutBuf(theFile);
- SetPort(savePort); /* go back to original port */
- ClosePort(dstPort);
- DisposPtr(dstPort);
- }
-
- /* Get the dimensions of the PICT, and write them as a BoundingBox
- comment */
- void WriteBoundingBox(PicHandle han, Rect *RectP, int theFile)
- {
- OutputString("%%BoundingBox: 0 0 ", theFile);
- OutputNum(RectP->right, theFile);
- OutputNum(RectP->bottom, theFile);
- OutputChar('\r',theFile);
- }
-
- /* Write out the prologue code of the file. For a real application, this
- should be gotten out of a resource file so it doesn't use up string space. */
- void WritePrologue(int theFile)
- {
- register int i;
- for (i=0;; i++) {
- if (prologueStrs[i][0] == 0) /* finished? */
- break;
- OutputString(prologueStrs[i], theFile);
- }
- }
-
- /* Flip the coordinate system so it matches the QuickDraw coordinates */
- void FlipCoords(Rect *RectP, int theFile)
- {
- OutputString("1 -1 scale ", theFile);
- OutputNum(0, theFile);
- OutputNum(-RectP->bottom, theFile);
- OutputString("translate ", theFile);
- }
-
-
- void WriteEpilogue(int theFile)
- {
- register int i;
- for (i=0;; i++) {
- if (epilogueStrs[i][0] == 0) /* finished? */
- break;
- OutputString(epilogueStrs[i], theFile);
- }
- }